<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
//        return $this->view->fetch();
        die;
        $this->word_turn_pdf('/uploads/cs.docx','/uploads/cs.pdf');
    }

    public function word_turn_pdf($wordPath, $pdfPath)
    {
        $filenamedoc = ROOT_PATH . 'public' . str_replace('/', DS, $wordPath);
        $filenamepdf = ROOT_PATH . 'public' . str_replace('/', DS, $pdfPath);



        // 原文件不存在则返回错误
        if (!file_exists($filenamedoc)) {
            return 'word原文件不存在';
        }
        // 输出目录不存在则创建目录
        if (!file_exists($tmpPath = rtrim($filenamepdf, basename($filenamepdf)))) {
            mkdir($tmpPath, 0777, true);
        }

        // 删除已有同名文件
        if (file_exists($filenamepdf)) {
            unlink($filenamepdf);
        }

        // 执行转换操作
        $word = new \COM("word.Application") or die("Could not initialise Object");
        $word->Documents->Open($filenamedoc);
        $word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
        $word->Quit(false);
        unset($word);

        // 在页面中显示生成的pdf
        // header('Content-type: application/pdf');
        // header('filename='.$filenamepdf);
        // readfile($filenamepdf);

        return $pdfPath;
    }
}
